/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

/*
 * SplashScreen.java
 *
 * Created on Dec 10, 2010, 7:40:38 PM
 */

package com.sowrov.gui.window;

import com.sowrov.util.logsystem.ChronicleLogger;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;

/**
 *
 * @author Sowrov
 */
public class SplashScreen extends javax.swing.JWindow {

    /** Creates new form SplashScreen */
    public SplashScreen(java.awt.Frame parent) {
        super(parent);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (dim.width-500)/2;
        int y = (dim.height-380)/2;
        initComponents();
        this.setLocation(x, y);
    }

    public void setStatus(final String str, final int progress) {
//        SwingUtilities.invokeLater(new Runnable() {
//            public void run() {
                int val = progress%101;
                statusLabel.setText(str);
                progressBar.setValue(val);
//            }
//        });
    }

    public void showSplash() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setVisible(true);
            }
        });
    }
    
    public void hideSplash() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setVisible(false);
                dispose();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        progressBar = new javax.swing.JProgressBar();
        statusLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        urlLabel = new javax.swing.JLabel();
        imageLabel = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));
        jPanel1.setAlignmentX(0.0F);
        jPanel1.setAlignmentY(0.0F);
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);

        progressBar.setBackground(new java.awt.Color(204, 204, 255));
        progressBar.setFont(new java.awt.Font("Tahoma", 0, 1));
        progressBar.setForeground(new java.awt.Color(0, 102, 204));
        progressBar.setBorderPainted(false);
        progressBar.setFocusable(false);
        progressBar.setMaximumSize(new java.awt.Dimension(32767, 5));
        progressBar.setMinimumSize(new java.awt.Dimension(10, 5));
        progressBar.setName("progressBar"); // NOI18N
        progressBar.setPreferredSize(new java.awt.Dimension(146, 5));

        statusLabel.setFont(new java.awt.Font("Tahoma", 0, 9));
        statusLabel.setForeground(new java.awt.Color(0, 51, 102));
        statusLabel.setText("starting...");
        statusLabel.setName("statusLabel"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 9));
        jLabel3.setForeground(new java.awt.Color(51, 51, 51));
        jLabel3.setText("<html>You get this application under Apache License V2. So it is \"<b>AS IS</b>\" basis, use it at your own risk"); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 9));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 9));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 9));
        jLabel7.setForeground(new java.awt.Color(51, 51, 51));
        jLabel7.setText("<html>You may obtain a copy of the License at  "); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        urlLabel.setFont(new java.awt.Font("Tahoma", 0, 9));
        urlLabel.setForeground(new java.awt.Color(51, 51, 51));
        urlLabel.setText("<html><a href=\"http://www.apache.org/licenses/LICENSE-2.0\">http://www.apache.org/licenses/LICENSE-2.0</a>"); // NOI18N
        urlLabel.setToolTipText("http://www.apache.org/licenses/LICENSE-2.0");
        urlLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        urlLabel.setName("urlLabel"); // NOI18N
        urlLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                urlLabelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(urlLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(127, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(statusLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(urlLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 210, 500, 60));

        imageLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/sowrov/afrigis/tools/app/timesheet_manager/resources/splash.jpg"))); // NOI18N
        imageLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        imageLabel.setAlignmentY(0.0F);
        imageLabel.setFocusable(false);
        imageLabel.setIconTextGap(0);
        imageLabel.setName("imageLabel"); // NOI18N
        imageLabel.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        getContentPane().add(imageLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 280));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void urlLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_urlLabelMouseClicked
        try {
            Desktop.getDesktop().browse(new URI(urlLabel.getToolTipText()));
        } catch (URISyntaxException ex) {
            ChronicleLogger.getInstance().logDebug("URI syntax error");
        } catch (IOException ex) {
            ChronicleLogger.getInstance().logDebug("IO error");
        }
    }//GEN-LAST:event_urlLabelMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        final SplashScreen dialog = new SplashScreen(new java.awt.Frame());
        dialog.showSplash();
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
        for (int i=1; i<100; i++) {
            try {
                dialog.setStatus(i + "% complete", i);
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(SplashScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JLabel urlLabel;
    // End of variables declaration//GEN-END:variables

}
